package org.linlinjava.litemall.admin.util;


import com.github.qcloudsms.SmsVoiceVerifyCodeSender;
import com.github.qcloudsms.httpclient.HTTPException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by houzb on 2016/6/7.
 */
public class SendMsgUtil {

    private static final String PostData = "sname=dlbaobei&spwd=ke5qEUt6&scorpid=&sprdid=";
    private static final  String  postUrl="http://cf.51welink.com/submitdata/Service.asmx/g_Submit";
    private static final Logger logger= LoggerFactory.getLogger(SendMsgUtil.class);

    public static String SMS(String phonenum,String msg) {
        return SMS(phonenum,msg,1012818);
    }
    public static String SMSSlow(String phonenum,String msg) {
        return SMS(phonenum,msg,1012812);
    }
    public static String SMS(String phonenum,String msg,int sprdid) {
        String sendInfo=PostData+sprdid+"&sdst="+phonenum+"&smsg=";
        try {
            sendInfo=sendInfo+ URLEncoder.encode(msg,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            logger.error("sendMsg-1exception---phonenum={},msg={}",phonenum,msg);
        }
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + sendInfo.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(sendInfo);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
            logger.error("sendMsg-exception---phonenum={},msg={}",phonenum,msg);
        }
        return "";
    }

    public static void main(String[] args) {
//        SendMsgUtil.SMS("15189179038","消息123123");

    }
}
